package com.example.mydesignmodule.celue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @author tgw
 * @date 2021/2/26
 * @describe
 */
class CalcultorRunMain {

    public static void main(String[] args) {
        CalculatorFactory calculatorFactory = new CalculatorFactory(new AddCalculator());
        System.out.println("加法："+calculatorFactory.CalculatorNum(1,2));
        calculatorFactory = new CalculatorFactory(new MultiplicationCalculator());
        System.out.println("乘法："+calculatorFactory.CalculatorNum(1,2));

        int[] nums =  new int[] {1,2,2};
        twoSum(nums,3);
        System.out.println("==："+ twoSum1(nums,6));
    }

    public static int[] twoSum(int[] nums, int target) {

        List<String> list = new ArrayList<>();

//        HashMap<Integer,Integer> hash = new HashMap<Integer,Integer>();
//        for(int i = 0; i < nums.length; i++) {
//            if (hash.containsKey(nums[i])) {
//                String total = "[" + i + "," + hash.get(nums[i]) + "]";
//                list.add(total);
//
//            }
//            hash.put(target-nums[i],i);
//        }


        for (int i=0;i<nums.length;i++){
            for (int j=i+1;j<nums.length;j++){
                if (nums[i]+nums[j]==target){
                    String total = "["+i+","+j+"]";
                    list.add(total);
                }
            }
        }
        for (String s:list){
            System.out.println("目标："+s);
        }
        return null;
    }

    public static int[] twoSum1(int[] nums, int target) {
        int m=0,n=0,k,board=0;
        int[] res=new int[2];
        int[] tmp1=new int[nums.length];
        System.arraycopy(nums,0,tmp1,0,nums.length);
        Arrays.sort(nums);
        for(int i=0,j=nums.length-1;i<j;){
            if(nums[i]+nums[j]<target)
                i++;
            else if(nums[i]+nums[j]>target)
                j--;
            else if(nums[i]+nums[j]==target){
                m=i;
                n=j;
                break;
            }
        }
        for(k=0;k<nums.length;k++){
            if(tmp1[k]==nums[m]){
                res[0]=k;
                break;
            }
        }
        for(int i=0;i<nums.length;i++){
            if(tmp1[i]==nums[n]&&i!=k)
                res[1]=i;
        }
        List<String> list = new ArrayList<>();
        String total = "["+ res[0]+","+  res[1]+"]";
        list.add(total);
        for (String s:list){
            System.out.println("目标=："+s);
        }
        return res;
    }


}
